home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8039 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  7.4 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: 29 Feb 1996 07:37:53 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb29003753@qcd.lanl.gov>
  8. References: <4gqpa1$3h9@alcor.usc.edu> <1996Feb26.211807.28858@isac.hces.com>
  9.     <31331a38.54160408@nntp.ix.netcom.com>
  10.     <1996Feb28.195423.10465@isac.hces.com>
  11. NNTP-Posting-Host: qcd.lanl.gov
  12. Mime-Version: 1.0
  13. Content-Type: text
  14. In-reply-to: gg@isac.hces.com's message of Wed, 28 Feb 1996 19:54:23 GMT
  15.  
  16. In article <1996Feb28.195423.10465@isac.hces.com>
  17. gg@isac.hces.com (Greg Goodrich) writes:
  18.  
  19. <snip>
  20. GG: : >     scanf("%s", &myarray);
  21. <snip>
  22. GG: : The %s format item in scanf expects a pointer to char.  myarray is
  23. GG: : converted to a pointer to char so it is legal.  &myarray is a pointer
  24. GG: : to array of 10 char and is not converted.  This results in undefined
  25. GG: : behavior.
  26. GG: 
  27. GG: : In many implementations pointer to char and pointer to array of 10
  28. GG: : char have the same representation and this will work properly, but
  29. GG: : this is not required by the standard.
  30. GG: 
  31. GG: I would like to see an example of how this could be implemented to fail.
  32.  
  33. This is a recurring point of philosophy which takes up a lot of time
  34. in this group. The point of a language standard is not only to
  35. standardize existing practice, but to draw up a charter which
  36.   1) defines which constructs have a meaning
  37.   2) either defines that meaning or specifies how that meaning is to
  38.      be defined.
  39. The existence of this charter gives the programmer to be guaranteed a basic
  40. minimum from an implementation that calls itself `conforming' to this
  41. standard: so that (s)he can write code without knowing which computer
  42. it will run on, and is guaranteed that no existing or future design
  43. `feature', no existing or future implementation on existing or future
  44. machine with existing or future architecture is going to reject the
  45. code, or to produce results which are not as defined in this charter;
  46. till the standard is changed incompatibly. The standards are rarely
  47. changed incompatibly in major ways, and when they are: one should
  48. consider it as a new language which supercedes the older one; in
  49. practice, both languages exist simultaneously for a while ... and to
  50. support legacy code, old compilers often exist for ever.
  51.  
  52. On the other hand, once you use features not defined by the standard,
  53. you are making assumptions about particular compilers, optimization
  54. techniques, hardware and architecture in writing the code. As it
  55. violates the defining document of the language, the code you write
  56. should not even be called as `written in that language'. So, whenever
  57. someone asks questions like `how could it fail?' in a discussion like
  58. this, I think that there is a philosophical trouble. Programming
  59. requires discipline, and rules however arbitrary have to be followed
  60. if those are the rules that define the language.
  61.  
  62. Remember, your compiler vendor is trying to implement only the defined
  63. features ... anything else may work or not work by accident. Remember
  64. that when a language is extended, it is usually extended in ways which
  65. do not conflict usages with defined meanings: very often it picks up
  66. precisely usages which do not have a defined meaning; and define them
  67. to have a meaning. Are you sure no extension, and no revision of the
  68. standard, is going to define this construct to have a meaning which
  69. you did not expect.
  70.  
  71. Further, you say,
  72.  
  73. GG: They both point to the exact same memory address, the only difference
  74. GG: being the datatype of the value, which can be type casted if necessary.
  75.  
  76. Remember a data type determines not only the range of values, but also
  77. their representation. Are you sure nobody will have any reason to
  78. define pointers to arrays to have a different representation than
  79. pointers to characters? Are you sure nobody will ever write a compiler
  80. that carries around extra information in array pointers so that it can
  81. do bounds checking? Are you sure if this information indeed exists, it
  82. will not mess up the stack when scanf actually accesses what it thinks
  83. is the character pointer itself?
  84.  
  85. Do you think there will never be a C implementation where the runtime
  86. system is smart enough to do type checks to determine whether the
  87. program makes sense? Are you sure C will never have smart linkers that
  88. try to check that the right version of the routine is being called
  89. based upon the number and tupes of the parameters?
  90.  
  91. If there were a type cast, the compiler would be obliged to change
  92. the representation if necessary. The type cast wasn't there: that is
  93. what lead to the undefined behaviour. All this discussion would not
  94. have started, if there were the cast. 
  95.  
  96. Actually, technically, the meaning of casting a pointer to an array to
  97. a pointer to its element type is `implementation defined'. Perhaps, by
  98. mistake, the standard does not specify that it has to point to the
  99. first (or as some people like to think of it, zeroth) element. I do
  100. not think anyone would have brought up this point in comp.lang.c
  101. ... the reason this discussion started is because there was _no
  102. cast_. 
  103.  
  104. If the cast were there, even if somebody posted a reply, they would
  105. have pointed out that as &myarray[0] has a defined meaning according
  106. to the standard, and (char*)&myarray is required to be defined by the
  107. implementation, it is better to use the first rather than the
  108. second. I also believe that casts are almost as bad as gotos: there
  109. are places where they must be used, but they are more commonly used to
  110. hide either bad design or, worse, errors.
  111.  
  112. GG: If you have an array, the address of the array is also the address of
  113. GG: the first member of the array, because the array itself is not a
  114. GG: pointer, but a reference.  The same goes for a structure.  The address
  115. GG: of a struct is the same as the address of the first member of the
  116. GG: struct, but of different data type.  One is pointer to struct and the
  117. GG: other is pointer to whatever the first member is declared as, but they
  118. GG: are both pointers, and therefore are the same size.
  119.  
  120. 1 is a number, so is 1.0 . Do you therefore conclude they have the
  121. same size??? If you read the FAQ, you will find plenty of examples of
  122. machines where not all pointers have the same size. Even with the same
  123. size, they need not have the same representation.
  124.  
  125. This is basically the mistake which people were trying to point
  126. out. Nowhere in the standard (or existing practice) do you find the
  127. requirement that a pointer is just an `address'. A pointer is all the
  128. information which along with its type suffices to fetch the object: it
  129. can have any information whatsoever that the implementation feels
  130. necessary. 
  131.  
  132. And by the way, the standard does mention that a pointer to a union
  133. _suitably cast_ points to the first member. A similar statement is
  134. also made for union types. Arrays are the only types left
  135. out. Nevertheless, for reasons stated above, I would rather use &str.x
  136. rather than (char*)&str when both are equivalent.
  137.  
  138. Cheers
  139. Tanmoy
  140. --
  141. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  142. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  143. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  144. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  145. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  146. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  147.